﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Data.Objects.DataClasses; 

namespace Fcdbas.Core.Serialization.Json
{
    /// <summary>
    /// Json扩展方法
    /// </summary>
    public static class JsonHelper
    {
        /// <summary>  
        /// 转换实体信息为Json  
        /// </summary>  
        /// <param name="obj">实体对象</param>  
        /// <returns></returns>  
        public static String ToEntitiesJson(this EntityObject obj)
        {
            return new EntitiesTools().EntitiesToJson(obj, "");
        }

        /// <summary>  
        /// 转换实体信息为Json  
        /// </summary>  
        /// <param name="obj">实体对象</param>  
        /// <param name="Include">获取子对象：使用，号分开</param>  
        /// <returns></returns>  
        public static String ToEntitiesJson(this EntityObject obj, String Include)
        {
            return new EntitiesTools().EntitiesToJson(obj, Include);
        }
        /// <summary>  
        /// 转换实体信息为Json  
        /// </summary>  
        /// <param name="obj">实体对象</param>  
        /// <returns></returns>  
        public static String ToEntitiesJson(this IQueryable obj)
        {
            return new EntitiesTools().EntitiesToJson(obj, "");
        }

        /// <summary>  
        /// 转换实体信息为Json  
        /// </summary>  
        /// <param name="obj">实体对象</param>  
        /// <param name="Include">获取子对象：使用，号分开</param>  
        /// <returns></returns>  
        public static String ToEntitiesJson(this IQueryable obj, String Include)
        {
            return new EntitiesTools().EntitiesToJson(obj, Include);
        }

        /// <summary>  
        /// 转换实体信息为Json  
        /// </summary>  
        /// <param name="obj">实体对象</param>  
        /// <param name="Include">获取子对象：使用，号分开</param>  
        /// <returns></returns>  
        public static String ToEntitiesJson(this IList obj, String Include)
        {
            return new EntitiesTools().EntitiesToJson(obj, Include);
        }  
        /// <summary>
        /// 把对象转换成JSON格式的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            
            return Encoding.UTF8.GetString(stream.ToArray());
        }
        /// <summary>
        /// 把Json字符串转换成对象
        /// </summary>
        /// <param name="jsonString"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object FromJson(this string jsonString,Type type)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
            MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            return serializer.ReadObject(mStream);
        }
    }
}
